home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3277 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.1 KB

  1. Path: access4.digex.net!not-for-mail
  2. From: ell@access4.digex.net (Ell)
  3. Newsgroups: comp.lang.c++
  4. Subject: [Q] [enums] class_ID as enum is basically use
  5. Date: 23 Jan 1996 05:22:15 GMT
  6. Organization: The Universe
  7. Message-ID: <4e1ra7$avi@news4.digex.net>
  8. NNTP-Posting-Host: access4.digex.net
  9. X-Newsreader: TIN [UNIX 1.3 950824BETA PL0]
  10.  
  11. (ell@access2.digex.net) wrote:
  12. : Robert C. Martin (rmartin@oma.com) wrote:
  13. : :...
  14. : : Often, you don't need a virtual function to expose the type, because
  15. : : the type is set in the constructor, and all objects derived from that
  16. : : the base class that defines the enum inherit the member variable.
  17. : : 
  18. : :     class X
  19. : :     {
  20. : :           public:
  21. : :             enum Type {x1, x2};
  22. : :             Type itsType;
  23. : :     };
  24. : : 
  25. : :         class X1 : public X
  26. : :         {
  27. : :            public:
  28. : :             X1() : itsType(x1) {}        
  29. : :         };
  30. : : 
  31. : :         class X2 : public X
  32. : :         {
  33. : :            public:
  34. : :            X2() : itsType(x2);
  35. : :         }
  36. : : 
  37. : :         X* xp = new X2;
  38. : :         assert(xp->itsType == X::x2);
  39.  
  40. : : The above example should convince you that a virtual function isn't
  41. : : necessary.  That any object derived from X will have the correct enum
  42. : : in its itsType variable, and that any client can get this variable
  43. : : through the X interface and interrogate it.
  44.  
  45. : While Lippman agrees with your point that derived classes should see a
  46. : public (or protected) data member of the base class, the VC++ compiler
  47. : version 1.5 does not.  The VC++ compiler gives the error that 'itsType' is
  48. : not a function or member of the derived classes.  I'm guessing that the
  49. : VC++ compiler is wrong.  Anyone care to straighten this out? 
  50. :
  51. : Elliott
  52.  
  53. Surprise!  When assignment rather than member initialization is used with
  54. Visual C++ 1.5, the above works.  Seems that while public parent data can
  55. be accessed by derived classes, as any other class can, contrary to the
  56. ARM of '92, they are not considered "inherited" members by VC 1.5.  So
  57. member initialization semantics can not be used.  Anyone care to try this
  58. on VC++ 4.0?  Would anyone care to explain this behavior by VC++ 1.5? 
  59.  
  60. Elliott
  61.